package ua.sumdu.LukyanovChalyi.Client;

import java.io.IOException;
import java.io.OutputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import ua.sumdu.LukyanovChalyi.Server.CopyBook;
import ua.sumdu.LukyanovChalyi.Utils.Transport;
import ua.sumdu.LukyanovChalyi.Utils.XMLUtils;

public class ToServerThread implements Runnable {

    private OutputStream out;
    private View view;

    public ToServerThread(OutputStream out, View view) {
        this.out = out;
        this.view = view;
    }

    @Override
    public void run() {
        try {
            while (true) {
                view.showActions();
                Integer choose = view.getAction();
                if (choose == null) {
                    continue;
                } else {
                    switch (choose.intValue()) {
                        case 1:
                            CopyBook cb = view.getCopyBook();
                            if (cb == null) {
                                break;
                            } else {
                                createXMLtoServer(Transport.ADD, cb);
                            }
                            break;
                        case 2:
                            Long id = view.getIDCopyBook();
                            Boolean active = null;
                            if (id == null || (active = view.getActive()) == null) {
                                break;
                            } else {
                                createXMLtoServer(Transport.SET_ACTIVE_BY_ID, id, active);
                            }
                            break;
                        case 3:
                            Long idRemove = view.getIDCopyBook();
                            if (idRemove == null) {
                                break;
                            } else {
                                createXMLtoServer(Transport.REMOVE_BY_ID, idRemove);
                            }
                            break;
                        case 4:
                            String writefile = view.getFileName();
                            if (writefile == null) {
                                break;
                            } else {
                                createXMLtoServer(Transport.WRITE_TO_FILE, writefile);
                            }
                            break;
                        case 5:
                            String readfile = view.getFileName();
                            if (readfile == null) {
                                break;
                            } else {
                                createXMLtoServer(Transport.READ_FROM_FILE, readfile);
                            }
                            break;
                        case 6:
                            createXMLtoServer(Transport.SHOW_BOOKS);
                            break;
                        case 7:
                            System.exit(0);
                            break;
                        default:
                            view.showMessage("Wrong choose. Try again!");
                            break;
                    }
                    Thread.sleep(100);
                }
            }
        } catch (InterruptedException e) {
            view.showException("ToServerThread was interrupted", e);
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
            } catch (IOException e) {
                view.showException("Can't close output stream!", e);
            }
        }
    }

    public void createXMLtoServer(String cmnd, Object... param) {
        try {
            XMLUtils utils = new XMLUtils();
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.newDocument();

            Element root = doc.createElement(XMLUtils.LIBRARY);
            Element command = doc.createElement(Transport.COMMAND);
            Element node = null;

            command.appendChild(doc.createTextNode(cmnd));
            root.appendChild(command);

            if (Transport.ADD.equals(cmnd)) {
                node = utils.makeCopyBook(doc, (CopyBook) param[0]);
            } else if (Transport.REMOVE_BY_ID.equals(cmnd)) {
                node = doc.createElement(Transport.ID);
                node.appendChild(doc.createTextNode(String.valueOf(param[0])));
            } else if (Transport.SET_ACTIVE_BY_ID.equals(cmnd)) {
                Element active = doc.createElement(Transport.SET_ACTIVE);
                active.appendChild(doc.createTextNode(String.valueOf(param[1])));
                root.appendChild(active);
                node = doc.createElement(Transport.ID);
                node.appendChild(doc.createTextNode(String.valueOf(param[0])));
            } else if (Transport.WRITE_TO_FILE.equals(cmnd) || Transport.READ_FROM_FILE.equals(cmnd)) {
                node = doc.createElement(Transport.FILENAME);
                node.appendChild(doc.createTextNode(String.valueOf(param[0])));
            }
            if (node != null) {
                root.appendChild(node);
            }

            doc.appendChild(root);
            utils.writeDocumentToStream(doc, out);

        } catch (TransformerException e) {
            view.showException("Some exception while writing messge to server", e);
        } catch (IOException e) {
            view.showException("Input / output exception", e);
        } catch (ParserConfigurationException e) {
            view.showException("Serious configuration error", e);
        }
    }
}